#
#
#

from argParser import *
import math
import os
import re
import sys
from types import *

numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
chars = ["(", ")", ";", ","]


ap = ArgParser(sys.argv)
tree_paths = ap.getArg("--input")
tokens = tree_paths.split(",")
t_paths = []
for t in tokens:
    #print t
    t = re.sub("\[", "", t)
    t = re.sub("\]", "", t)
    t_paths.append(t)

comp_tree_parts = {}

count_trees = 0
for p in t_paths:
    comp_tree_parts[count_trees] = []
    fin = open(p, "r")
    for line in fin.readlines():
        treestring = line
        #print treestring
        tokens = treestring.split(":")    
        comp_tree_parts[count_trees] = tokens
        count_parts = 0
        for t in comp_tree_parts[count_trees]:
            startswith_number = False
            for n in numbers:
                if t.startswith(n):
                    startswith_number = True
                    break
            if startswith_number == True:
                #print t
                comp_tree_parts[count_trees][count_parts] = []
                this_index = 0
                for char in t:
                    if chars.__contains__(char):
                        #print t
                        comp_tree_parts[count_trees][count_parts].append( t[0:this_index] )
                        comp_tree_parts[count_trees][count_parts].append( t[this_index:] )
                        #print "-->", t[0:this_index], t[this_index:]
                        break
                    this_index += 1
            count_parts += 1
            #print "TREE ", count_trees, comp_tree_parts
        count_trees += 1
    fin.close()
            
# eliminate heirarachical structure of parts list
component_tree_pieces = {}
for i in range(0, comp_tree_parts.__len__() ):
    component_tree_pieces[i] = []
    for j in range(0, comp_tree_parts[i].__len__() ):
        if type(comp_tree_parts[i][j]) == ListType:
            for el in comp_tree_parts[i][j]:
                component_tree_pieces[i].append(el)    
        else:
            component_tree_pieces[i].append(comp_tree_parts[i][j])
#print "pieces...", component_tree_pieces

final_tree_string = ""

for j in range(0, component_tree_pieces[0].__len__()): # for each piece
    piece_is_bl = False
    
    for i in range(0, comp_tree_parts.__len__()):
        if component_tree_pieces[0][j] != component_tree_pieces[i][j]:
            piece_is_bl = True
    
    if piece_is_bl:
        final_tree_string += ":["
        for i in range(0, comp_tree_parts.__len__()):
            final_tree_string += component_tree_pieces[i][j].__str__() + ","
        final_tree_string = re.sub(",$", "", final_tree_string)
        final_tree_string += "]"
    else:
        final_tree_string += component_tree_pieces[0][j].__str__()

print final_tree_string

